
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Reflection;

namespace needle.UI {

    public delegate void DelegateCloseSplash();

    public class Splash : System.Windows.Forms.Form {

        #region Private variables

        private static Splash m_instance;
        private static String m_imageFile;
        private static Color m_transColor;
        private Bitmap m_bmp;
        private DelegateCloseSplash m_delegateClose;

        private System.ComponentModel.Container components = null;
        private Label title;
        private Label about;
        private PictureBox pictureBoxCredits;
        public bool License_Valid = false;

        #endregion

        public Splash() {
            InitializeComponent();
        }
        public Splash(String name, Color col)
        {
            //Debug.Assert(imageFile != null && imageFile.Length > 0, "A valid file path has to be given");
            // ====================================================================================
            // Setup the form
            // ==================================================================================== 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ShowInTaskbar = false;
            this.TopMost = true;

            // make form transparent
            this.TransparencyKey = this.BackColor;

            // tie up the events
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Splash_KeyUp);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Splash_Paint);
            this.MouseDown += new MouseEventHandler(Splash_MouseClick);

            // load and make the bitmap transparent
            Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            Stream myStream = myAssembly.GetManifestResourceStream("needle.UI." + name);
            m_bmp = new Bitmap(myStream);
            //m_bmp = new Bitmap(imageFile);

            if(m_bmp == null)
                throw new Exception("Failed to load the bitmap file " + name);
            m_bmp.MakeTransparent(col);

            // resize the form to the size of the iamge
            this.Width = m_bmp.Width;
            this.Height = m_bmp.Height;

            // center the form
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            // thread handling
            m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);
        }
        protected override void Dispose(bool disposing) {
            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent() {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Splash));
            this.title = new System.Windows.Forms.Label();
            this.about = new System.Windows.Forms.Label();
            this.pictureBoxCredits = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCredits)).BeginInit();
            this.SuspendLayout();
            // 
            // title
            // 
            this.title.Font = new System.Drawing.Font("Copperplate Gothic Bold", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.title.ForeColor = System.Drawing.Color.DarkOliveGreen;
            this.title.Location = new System.Drawing.Point(12, 172);
            this.title.Name = "title";
            this.title.Size = new System.Drawing.Size(294, 16);
            this.title.TabIndex = 34;
            this.title.Text = "needle";
            this.title.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // about
            // 
            this.about.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.about.Location = new System.Drawing.Point(12, 188);
            this.about.Name = "about";
            this.about.Size = new System.Drawing.Size(294, 16);
            this.about.TabIndex = 33;
            this.about.Text = "by identy";
            this.about.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // pictureBoxCredits
            // 
            this.pictureBoxCredits.BackColor = System.Drawing.Color.White;
            this.pictureBoxCredits.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBoxCredits.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxCredits.Image")));
            this.pictureBoxCredits.Location = new System.Drawing.Point(154, 12);
            this.pictureBoxCredits.Name = "pictureBoxCredits";
            this.pictureBoxCredits.Size = new System.Drawing.Size(152, 150);
            this.pictureBoxCredits.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pictureBoxCredits.TabIndex = 32;
            this.pictureBoxCredits.TabStop = false;
            this.pictureBoxCredits.WaitOnLoad = true;
            // 
            // Splash
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(318, 213);
            this.ControlBox = false;
            this.Controls.Add(this.title);
            this.Controls.Add(this.about);
            this.Controls.Add(this.pictureBoxCredits);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimizeBox = false;
            this.Name = "Splash";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.TopMost = true;
            this.Load += new System.EventHandler(this.Splash_Load);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Splash_Paint);
            this.Activated += new System.EventHandler(this.Splash_Activated);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCredits)).EndInit();
            this.ResumeLayout(false);

        }
        private void Splash_Load(object sender, System.EventArgs e) { }
        //private void Splash_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { }
        private void Splash_Activated(object sender, System.EventArgs e) { }

        #region Public methods
        // this can be used for About dialogs
        public static void ShowModal(String imageFile, Color col)
        {
            m_imageFile = imageFile;
            m_transColor = col;
            MySplashThreadFunc();
        }
        // Call this method with the image file path and the color 
        // in the image to be rendered transparent
        public static void StartSplash(String imageFile, Color col)
        {
            m_imageFile = imageFile;
            m_transColor = col;
            // Create and Start the splash thread
            Thread InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
            InstanceCaller.Start();
        }

        // Call this at the end of your apps initialization to close the splash screen
        public static void CloseSplash()
        {
            if (m_instance != null)
                m_instance.Invoke(m_instance.m_delegateClose);

        }
        #endregion // Public methods

        #region Dispose
        protected override void OnClosed(EventArgs e)
        {
            m_bmp.Dispose();
            base.OnClosed(e);
            m_instance = null;
        }
        //protected override  void Dispose(bool disposing)
        //{
        //    m_bmp.Dispose();
        //    base.Dispose(disposing);
        //    m_instance = null;
        //}
        #endregion // Dispose

        #region Threading code
        // ultimately this is called for closing the splash window
        void InternalCloseSplash()
        {
            this.Close();
            this.Dispose();
        }
        // this is called by the new thread to show the splash screen
        private static void MySplashThreadFunc()
        {
            m_instance = new Splash(m_imageFile, m_transColor);
            m_instance.TopMost = false;
            m_instance.ShowDialog();
        }
        #endregion // Multithreading code

        #region Event Handlers

        void Splash_MouseClick(object sender, MouseEventArgs e)
        {
            this.InternalCloseSplash();
        }

        private void Splash_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawImage(m_bmp, 0, 0);
        }

        private void Splash_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.InternalCloseSplash();
        }
        #endregion // Event Handlers

    }
}